package com.lizhi.note.service.impl;

import com.lizhi.note.dto.NotepageDTO;
import com.lizhi.note.entity.NotepageEntity;
import com.lizhi.note.entity.NotepageHistoryEntity;
import com.lizhi.note.mapper.NotepageMapper;
import com.lizhi.note.repository.NotepageHistoryRepository;
import com.lizhi.note.repository.NotepageRepository;
import com.lizhi.note.service.NotepageHistoryService;
import com.lizhi.note.service.NotepageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Transactional
public class NotepageServiceImpl implements NotepageService {

    private final NotepageRepository notepageRepository;
    private final NotepageMapper notepageMapper;

    @Autowired
    private NotepageHistoryService notepageHistoryService;

    public NotepageServiceImpl(NotepageRepository notepageRepository, NotepageMapper notepageMapper) {
        this.notepageRepository = notepageRepository;
        this.notepageMapper = notepageMapper;
    }

    @Override
    public NotepageDTO createNotepage(NotepageDTO notepageDto) {
        NotepageEntity entity = notepageMapper.toEntity(notepageDto);
        NotepageEntity savedEntity = notepageRepository.save(entity);
        return notepageMapper.toDto(savedEntity);
    }

    @Override
    public Optional<NotepageDTO> getNotepageById(Long id) {
        return notepageRepository.findById(id)
                .map(notepageMapper::toDto);
    }

    @Override
    public List<NotepageDTO> getAllNotepages() {
        return notepageRepository.findAll()
                .stream()
                .map(notepageMapper::toDto)
                .collect(Collectors.toList());
    }

    /**
     * 先在history表里进行保存历史版本，然后再更新
     * @param notepageDto
     * @return
     */
    @Transactional
    @Override
    public NotepageDTO updateNotepage(NotepageDTO notepageDto) {
        NotepageEntity entity = notepageRepository.findById(notepageDto.getId())
                .orElseThrow(() -> new IllegalArgumentException("Notepage not found with id: " + notepageDto.getId()));

        // 保存历史记录
        NotepageHistoryEntity historyEntity = new NotepageHistoryEntity(entity);
        notepageHistoryService.create(historyEntity);

        notepageMapper.updateEntity(notepageDto, entity);

        NotepageEntity updatedEntity = notepageRepository.save(entity);
        return notepageMapper.toDto(updatedEntity);
    }

    @Override
    public void deleteNotepage(Long id) {
        notepageRepository.deleteById(id);
    }

    @Override
    public Optional<NotepageDTO> findByTitle(String title) {
        return notepageRepository.findByTitle(title)
                .map(notepageMapper::toDto);
    }

    @Override
    public List<NotepageDTO> getNotepagesByNotebookId(Long notebookId) {
        List<NotepageEntity> entities = notepageRepository.findByNotebookIdOrderByUpdateTimeDesc(notebookId);
        return notepageMapper.toDtoList(entities);
    }
}
